<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/14 00:15
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Common_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加
     * @param $params
     * @return array
     */
    public function add($params)
    {
        $data = $this->formatData($params);
        $this->db->insert('base', $data);
        $id = $this->db->insert_id();
        return array_merge($data, array('id' => $id));
    }

    /**
     * 编辑
     * @param array $params
     * @return bool
     */
    public function edit($params = array())
    {
        $baseId = $params['baseId'];
        $cDb = clone($this->db);
        $cDb->where('id', $baseId);
        $data = $this->formatData($params);
        $cDb->update('base', $data);
        if ($cDb->affected_rows()) {
            return true;
        }
        return false;
    }

    /**
     * 格式化
     * @param array $params
     * @param string $type
     * @return array
     */
    private function formatData($params = array(), $type = 'add')
    {
        $data = array(
            'name' => value($params, 'baseName', ''),
            'charger' => value($params, 'charger', ''),
            'phone' => value($params, 'phone', ''),
            'telephone' => value($params, 'telephone', ''),
            'liveUrl' => value($params, 'liveUrl', ''),
            'address' => value($params, 'address', ''),
            'content' => value($params, 'content', ''),
            'thumb' => value($params, 'thumb', ''),
            'streamId' => value($params, 'streamId', 0),
            'video' => value($params, 'video', ''),
            'status' => 1,
            'isShow' => value($params, 'slider') == 'true' ? 1 : 0
        );

        $adminId = adminId();
        $dateTime = dateTime();
        if ($type == 'add') {
            return array_merge($data, array(
                'created' => $dateTime,
                'createdBy' => $adminId,
                'modified' => $dateTime,
                'modifiedBy' => $adminId,
            ));
        } else {
            return array_merge($data, array(
                'modified' => $dateTime,
                'modifiedBy' => $adminId
            ));
        }
    }

    /**
     * 获取列表
     * @param int $offset
     * @param int $limit
     * @return bool
     */
    public function getLists($offset = 0, $limit = 20)
    {
        try {
            $cloneDb = clone($this->db);
            $cloneDb->where(array('status' => 1));
            $total = $cloneDb->count_all_results('base');

            if (empty($total)) {
                return false;
            }

            $q = $this->db->order_by('id', 'DESC')
                ->select('id, name, charger, phone, telephone, liveUrl, address, thumb, streamId, video, createdBy, created, status, isShow')
                ->limit($limit, $offset)
                ->get_where('base', array('status' => 1));
            $row = $q->result_array();
            if (empty($row)) {
                return false;
            }
            return array(
                'total' => $total,
                'data' => $row
            );
        } catch (PDOException $e) {
            $this->setError('查询错误');
            return false;
        }
    }

    /**
     * 获取详情
     * @param $baseId
     * @return mixed
     */
    public function getDetail($baseId)
    {
        $cloneDb = clone($this->db);
        $q = $cloneDb->get_where('base', array('id' => $baseId));
        return $q->row_array();
    }

    /**
     * 检查是否存在该基地
     * @param $baseId
     * @return bool
     */
    public function checkExists($baseId)
    {
        $cloneDb = clone($this->db);
        $baseId = intval($baseId);
        $q = $cloneDb->get_where('base', "id = {$baseId} and status != -1");
        $r = $q->row_array();
        return !empty($r);
    }

    /**
     * 最新农场
     * @param int $limit
     * @return mixed
     */
    public function getLatestBases($limit = 20)
    {
        $query = $this->db->select('id, name, thumb, charger, address, created')
            ->limit($limit)
            ->order_by('created', 'DESC')
            ->from('base')->get();
        return $query->result_array();
    }
}